var templageContnetJs = require('t_content/t_content.js');
var apiFun = require('../../utils/api.js');

var data ={
  "magicId": 4,
  "title": "好像听撩人的话",
  "createTime": "2018-07-05 17:05:17",
  "colorScheme": {
    "themeId": 2,
    "themeName": "黄色系",
    "isDelete": 1,
    "bgColor": "-webkit-linear-gradient(top, rgb(234,185,45) 0%,rgb(199,152,16) 100%)",
    "navBgColor": "#E5B900",
    "navFontColor": "#ffffff",
    "qBgColor": "#ffffff",
    "qAuthorColor": "#ffffff",
    "qContentFontColor": "black",
    "aBgColor": "#ffffff",
    "aAuthorColor": "#ffffff",
    "aContentFontColor": "black",
    "buttonFontColor": "black",
    "buttonBgColor": "rgba(255, 255, 255, 0.5)"
  }
};

var defaultTheme={
    "themeId": 2,
    "themeName": "黄色系",
    "isDelete": 1,
    "bgColor": "whtie",
    "navBgColor": "white",
    "navFontColor": "black",
    "qBgColor": "#ffffff",
    "qAuthorColor": "#ffffff",
    "qContentFontColor": "black",
    "aBgColor": "#ffffff",
    "aAuthorColor": "#ffffff",
    "aContentFontColor": "black",
    "buttonFontColor": "black",
    "buttonBgColor": "rgba(255, 255, 255, 0.5)"
}

var footerConfig = {
  'isFooterMagic': false,
  'isFooterZb': true,
  'fontColor': '#3D3D3D',
  'lineColor': '#3D3D3D',
  'fontSelectedColor': '#3D3D3D',
}

Page({

  onPullDownRefresh: function () {
    var isMagicDescribe = this.data.isMagicDescribe;

    if (this.data.isShowAnswerMagic == true) {
        this.setData({
          isLoading:false
        })
    }

    /*
    wx.startPullDownRefresh({
        success:function(res){
          console.log('12');
        }
    })*/
  },

  //init data
  data:{
    list: {},
    animationMagicTopic:{},
    animationIndex:0,
    isShowAnswerMagic:true,
    isLoading: false,
    footerConfig: footerConfig,
    defaultTheme: defaultTheme
  },

  //onLoad
  onLoad:function(event){


  },

  //onShow
  onShow: function () {
    console.log('切换1');
    
    var list = this.data.list;
    
    if (Object.keys(list).length != 0 ){
      wx.setTabBarStyle({
        color: '#ffffff',
        selectedColor: '#EDEDED',
        backgroundColor: list.colorScheme.navBgColor,
        borderStyle: 'white',
        fail: function (res) {
          console.log('qh1 :1');
          console.log(res);
          console.log(list.colorScheme.navBgColor);
        }
      })
    }else {
      wx.setTabBarStyle({
        color: '#000000',
        selectedColor: '#000000',
        backgroundColor: '#ffffff',
        borderStyle: 'white',
        fail: function (res) {
          console.log('qh1 :2');
          console.log(res);
        }
      })
    }

    //animation 简介
    var animationMagicTopic = wx.createAnimation({
      transformOrigin: "50% 50%",
      duration: 1000,
      timingFunction: "ease",
      delay: 0
    })

    this.animationMagicTopic = animationMagicTopic 
    this.setData({
      animationMagicTopic: animationMagicTopic.export()
    })

    //animation MagicContent 内容
    var animationMagicContent = wx.createAnimation({
      transformOrigin: "50% 50%",
      duration: 1000,
      timingFunction: "ease",
      delay: 0
    })

    this.animationMagicContent = animationMagicContent
    this.setData({
      animationMagicContent: animationMagicContent.export(),
    })

  },

  //lock Magic
  lockMagic:function(event){

    var currentThemeId = '';
    var thisList = this.data.list;
    var that = this;

    if (thisList.colorScheme) {
      currentThemeId = thisList.colorScheme.themeId;
    }

    this.setData({
      isLoading: true,
    })
  
    //api requeset divination
    apiFun.getDivinationList(
      {
        userId: 1,
        currentThemeId: currentThemeId,
        
        success: function (res) {

          //set view content 
          res.list.divinationShowContent = res.list.divinationContent;

          //set first tarbar bg color and font color
          if (res.list != false) {
            var firstBgColor = res.list['colorScheme']['navBgColor'];
            var firstFrontColor = res.list['colorScheme']['navFontColor'];
            
            wx.setNavigationBarColor({
              frontColor: firstFrontColor,
              backgroundColor: firstBgColor,
              animation: {
                duration: 400,
                timingFunc: 'easeIn'
              }
            })

            //设置底部颜色
            wx.setTabBarStyle({
              color: '#ffffff',
              selectedColor: '#EDEDED',
              backgroundColor: firstBgColor,
              borderStyle: 'white',
              fail: function (res) {
                console.log(res);
              }
            })
          } 

      
          that.setData({
            list: res.list,
            loadingMagic: false,
            isLoading:false,
          })



          //问题滑动
          var systemInfo = wx.getSystemInfoSync();
          that.animationMagicTopic.translateY(-780 / 750 * systemInfo.windowWidth).step();
          that.setData({
            animationMagicTopic: that.animationMagicTopic.export(),
          })

          //答案滑动
          that.animationMagicContent.translateY(- 840 / 750 * systemInfo.windowWidth).step();
          that.setData({
            animationMagicContent: that.animationMagicContent.export()
          })

          that.setData({
            isShowAnswerMagic: false
          })

        },
        
        fail:function(res){
          console.log(1);
          return false;
        }
      }
    )


  },

  //lock Magic
  closeAnswerMagic: function (event) {

    var systemInfo = wx.getSystemInfoSync();
    this.animationMagicTopic.translateY(10 / 750 * systemInfo.windowWidth).step();

    this.setData({
      animationMagicTopic: this.animationMagicTopic.export(),
    })

    //旧的上去
    this.animationMagicContent.translateY(670 / 750 * systemInfo.windowWidth).step();
    this.setData({
      animationMagicContent: this.animationMagicContent.export()
    })

    //var newList = this.data.list;
    //newList['colorScheme'] = defaultTheme;

    this.setData({
      isShowAnswerMagic: true
    })


  },
  
  jumpMagic: function (event) {
    wx.redirectTo({
      url: '/pages/mc_new_u/mc'
    })
  },
  JumpZb: function (event) {
    wx.redirectTo({
      url: '/pages/zb/zb'
    })
  }


})